package com.javacoo.limit.client.handler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang3.StringUtils;

import com.javacoo.limit.client.config.LimitRule;

import lombok.extern.slf4j.Slf4j;

/**
 * 限流配置处理器
 * <li>处理配置方式限流</li>
 *
 * @author: duanyong@jccfc.com
 * @since: 2021/5/23 17:06
 */
@Slf4j
public class ConfigLimitHandler extends AbstractLimitHandler<Object> implements MethodInterceptor {
    @Nullable
    @Override
    public Object invoke(@Nonnull MethodInvocation methodInvocation) throws Throwable {
        // 获取被拦截的类名
        String className = methodInvocation.getClass().getName();
        // 获取被拦截的方法名
        String methodName = methodInvocation.getMethod().getName();
        String defaultKey = StringUtils.join(className,".", methodName);
        //获取配置文件中的限流规则
        LimitRule limitRule = lockConfig.getLimitRule();
        //如果需要特殊处理
        if(lockConfig.getLimitMethodMap().containsKey(methodName)){
            limitRule = lockConfig.getLimitMethodMap().get(methodName);
        }
        limitRule.setDefaultKey(defaultKey);
        log.info("[ConfigLimitHandler限流交易请求],尝试获取方法:{},执行权限", defaultKey);
        return handle(limitRule, () -> {
            try {
                return methodInvocation.proceed();
            } catch (Throwable throwable) {
                log.error("[ConfigLimitHandler限流交易请求],方法执行异常",throwable);
            }
            return null;
        });
    }
}
